package com.baoming.web.action;
  
import java.util.List; 

import javax.servlet.http.Cookie;
  
import com.baoming.container.ServiceContainer;
import com.baoming.domain.Article;
import com.baoming.domain.Member;
import com.baoming.domain.Properties;
import com.baoming.domain.Singup;
import com.baoming.domain.Site;
import com.baoming.service.IArticleService;
import com.baoming.service.IPropertiesService;
import com.baoming.service.ISingupService;
import com.baoming.service.ISiteService;
import com.baoming.tool.MatrixTool;
import com.baoming.tool.MemberTool;
import com.baoming.util.PageBeanUtil;
import com.baoming.util.PageInfo;
import com.baoming.util.ServiceTool;
import com.baoming.util.Struts2UploadUtil;
import com.baoming.util.UploadUtil;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public class SiteAction extends BaseAction implements ModelDriven<Site> {

	private ISiteService siteService = (ISiteService) ServiceContainer.getService(ISiteService.SERVICE) ; 
	private IArticleService articleService = (IArticleService) ServiceContainer.getService(IArticleService.SERVICE) ; 
	private ISingupService singupService = (ISingupService) ServiceContainer.getService(ISingupService.SERVICE) ; 
	private IPropertiesService propService = (IPropertiesService) ServiceContainer.getService(IPropertiesService.SERVICE) ; 
	private Site siteModel = new Site() ; 
	private String codeResult ; 
	
	public String getCodeResult() {
		return codeResult;
	}

	public void setCodeResult(String codeResult) {
		this.codeResult = codeResult;
	}

	/**
	 * 网站标识码
	 */
	public String urlcode(){
		
		//得到标识码
		String siteCode = request.getParameter("siteCode") ; 
		//得到站点的id
		String id = request.getParameter("id") ; 
		String tip;
		//验证标识码是否合法
		if(siteCode != null && siteCode.length() > 0 && siteCode.length() < 15){
			//查询标识码是否重复
			Site site = siteService.findSiteByCode(siteCode) ;
			//如果不存在 则保存
			if(site == null){
				site = siteService.findSite(id) ; 
				if(site != null){
					//更新站点标识码
					siteService.updateSiteCode(siteCode , id) ; 
					tip = "4" ;  
				}else{
					tip = "3" ; 
				}
			}else {
				//返回错误标识码2
				tip = "2" ;  
			}
		}else{
			//返回错误标识码1
			tip = "1" ;  
		}
		request.setAttribute("tip", tip) ; 
		return "urlcode" ; 
	}
	
	@Override
	public String add() {
		return null;
	}
	
	/*
	 * 分配报名
	 */
	public String sing(){
		 
		String urlPath = request.getRequestURI().toString() ;  
		int startInt = urlPath.indexOf("_") ; 
		int endInt = urlPath.indexOf(".") ; 
		//得到报名的ID
		String singId = urlPath.substring(startInt+1, endInt) ;  
		
		//验证用户报名 cookie 
		Cookie cookie = ServiceTool.getCookieByName(request, "isSingUp") ; 
		
		if(cookie != null && request.getParameter("reSing")== null){
			if(cookie.getValue().equals(singId)){
				request.setAttribute("error", "5") ; 
				request.setAttribute("singupId", singId) ; 
				return ERROR ; 
			}
		}
		
		//查询得到报名的信息
		Singup singup = singupService.selectSingUpById(singId) ; 
		//时间对比
		int state = ServiceTool.timeCompain(singup.getEndTime(), ServiceTool.getNowDate()) ; 
		//报名结束
		if(state == 0){
			//输入站点信息到界面 
			request.setAttribute("memberId" , singup.getMemberId()) ; 
			return "singOver" ; 
		}
		
		request.setAttribute("singup", singup) ; 
		
		//通过会员的ID查询出站点的信息
		String memberId = singup.getMemberId() ; 
		Site site = siteService.findSiteByMember(memberId) ; 
		request.setAttribute("site", site) ; 
		
		//通过报名的ID查询出其比赛的属性值 
		List<Properties> props = propService.getPropertiesBySingupId(singId) ;  
		request.setAttribute("props", props) ; 
		return "sing" ; 
	}
	
	/*
	 * 得到站点信息
	 */
	private boolean siteMsg(String memberId){
		Site site = siteService.findSiteByMember(memberId); 
		
		if(site == null){
			return false ;
		}
		request.setAttribute("site", site) ; 
		return true ; 
	}
	
	/**
	 * 站点分配 
	 * @return
	 */
	public String distribution(){
		
		String urlPath = request.getRequestURI().toString() ;  
		int startInt = urlPath.indexOf("_") ; 
		int endInt = urlPath.indexOf(".") ; 
		//得到所以会员的ID
		String id = urlPath.substring(startInt+1, endInt) ; 
		 
		String memberId = id ; 
		 
		//判断id的长度 如果大于等于30位 则为uuid 
		if(id.length() >= 30){
			//查询用户是否建立站点信息
			if(!siteMsg(memberId)){ 
				//1 表示用户没有建立站点信息
				request.setAttribute("tip", 1) ; 
				return "distributionError" ; 
			}
		}
		//否则为站点的唯一码
		else{
			//得到站点的信息
			Site site = siteService.findSiteByCode(id) ; 
			if(site == null){
				//1 表示用户没有建立站点信息
				request.setAttribute("tip", 1) ; 
				return "distributionError" ; 
			}
			//得到会员的id 
			memberId = site.getMemberId() ; 
			//设置站点信息到页面 
			request.setAttribute("site", site) ; 
		}
		 
		PageInfo pageInfo = new PageInfo(request) ; 
		List<Article> articles = articleService.selectArticleWithSort(pageInfo, memberId) ; 
		//得到会员所有信息
		request.setAttribute("articles", articles) ; 
		PageBeanUtil.setPage(pageInfo, request) ; 
		
		//得到所有的比赛信息
		List<Singup> singups = singupService.selectSingupWithPage(memberId) ; 
		request.setAttribute("singups", singups) ; 
		
		return "distribution" ; 
	}
	
	/**
	 * 站点修改
	 * @return
	 */
	public String modify(){ 
		
		//得到会员的ID
		String memberId = MemberTool.getGlobalMemberId(request) ; 
		 
		//查询出修改的站点信息
		Site site = siteService.findSiteByMember(memberId) ; 
		  
		String picPath = null ; 
		
		//图标 
		if(Struts2UploadUtil.fileUtil(siteModel.getPic())){
			picPath = UploadUtil.uploadSimpleFile(siteModel.getPic(), siteModel.getPicFileName(), "upload") ; 
		}
		
		/*
		 *	如果用户没有建立站点 则建立 
		 */
		if(site == null){ 
			
			//生成二维码
			String codePath = createCode(memberId) ; 
			siteModel.setCodePath(codePath) ; 
			
			/*
			 * 设置公共信息
			 */
			siteModel.setMemberId(memberId) ;  
			siteModel.setIsDelete(1) ;  
			siteModel.setPicPath(picPath) ; 
			
			siteService.buildSite(siteModel) ; 
			
		}
		/*
		 *  站点已经存在 则修改站点信息
		 */
		else{ 
			
			//更新站点信息
			if(picPath != null){
				site.setPicPath(picPath) ; 
			}
			//更新二维码
			String codePath = createCode(memberId) ; 
			siteModel.setCodePath(codePath) ; 
			site.setCopyRight(siteModel.getCopyRight()) ; 
			site.setDescribes(siteModel.getDescribes()) ; 
			site.setName(siteModel.getName()) ; 
			
			siteService.modifySite(site) ; 
		}
		
		return "modify" ; 
	}
	
	//生成二维码
	private String createCode(String memberId){
		
		int width = 500 ; 
		int height = 500 ; 
		String url = "distribution_"+memberId+".action" ; 
		String path = MatrixTool.craete(width, height, url, memberId) ;  
		
		return path ;
		
	}

	@Override
	public String edit() {
		
		Member gmember = MemberTool.getGlobalMember(request) ; 
		String memberId = gmember.getId() ; 
		Site site = siteService.findSiteByMember(memberId) ;
		
		if(site == null){
			request.setAttribute("tip", "error") ; 
		}else{
			request.setAttribute("tip", "success") ; 
			request.setAttribute("site", site) ; 
		}
		
		return SUCCESS ;
	}

	@Override
	public String manager() {
		return null;
	}

	@Override
	public String remove() {
		return null;
	}

	@Override
	public String removeGroup() {
		return null;
	}

	public Site getModel() {
		return siteModel;
	}

}
